/*
 * Copyright 2019 Zhang XueHai
 * you may not use this file except in compliance with the License.
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.zhangxuehai.android.pdfviewer.annotation.pen;

import androidx.annotation.ColorInt;

import com.google.gson.Gson;

/**
 * 画笔构建参数较多，且画笔数量多。采用构建模式简化
 * @author 章学海
 * @date 2019/12/31
 */
public final class PenBuilder {
    public static final Pen getPen(PenType penType, String jsonData, Gson gson){
        Pen pen = null;
        switch (penType){
            case COLOR:
                pen = gson.fromJson(jsonData,ColorPen.class);
                break;
            case BRUSH:
                pen = gson.fromJson(jsonData,BrushPen.class);
                break;
            case SELECTED:
                pen = gson.fromJson(jsonData,SelectedPen.class);
                break;
            case STRIKETHROUGH:
                pen = gson.fromJson(jsonData,StrikethroughPen.class);
                break;
            case UNDERLINE:
                pen = gson.fromJson(jsonData,UnderlinePen.class);
                break;
            default:
                throw new RuntimeException("错误的画笔类型");
        }
        pen.init();
        return pen;
    }

    public static ColorPenBuilder colorPenBuilder(){
        return new ColorPenBuilder();
    }
    public static BrushPenBuilder brushPenBuilder(){
        return new BrushPenBuilder();
    }
    public static SelectedPenBuilder selectedPenBuilder(){
        return new SelectedPenBuilder();
    }
    public static StrikethroughPenBuilder strikethroughPenBuilder(){
        return new StrikethroughPenBuilder();
    }
    public static UnderlinePenBuilder underlinePenBuilder(){
        return new UnderlinePenBuilder();
    }

    static abstract class Builder{
        abstract public Pen build();
    }

    public static final class ColorPenBuilder extends Builder{
        ColorPenBuilder() {}
        int color;
        float penWidthScale;
        public ColorPenBuilder setColor(@ColorInt int color){
            this.color = color;
            return this;
        }
        public ColorPenBuilder setPenWidthScale(float penWidthScale) {
            this.penWidthScale = penWidthScale;
            return this;
        }

        @Override
        public Pen.WritePen build() {
            return new ColorPen(color,penWidthScale);
        }
    }

    public static final class BrushPenBuilder extends Builder{
        BrushPenBuilder() {}
        int color;
        float penWidthScale;
        public BrushPenBuilder setColor(@ColorInt int color){
            this.color = color;
            return this;
        }
        public BrushPenBuilder setPenWidthScale(float penWidthScale) {
            this.penWidthScale = penWidthScale;
            return this;
        }

        @Override
        public Pen.WritePen build() {
            return new BrushPen(color,penWidthScale);
        }
    }

    public static final class SelectedPenBuilder extends Builder{
        SelectedPenBuilder() {}

        int color;

        public SelectedPenBuilder setColor(int color) {
            this.color = color;
            return this;
        }

        @Override
        public SelectedPen build() {
            return new SelectedPen(color);
        }
    }

    public static final class StrikethroughPenBuilder extends Builder{
        StrikethroughPenBuilder() {}

        int color;

        public StrikethroughPenBuilder setColor(int color) {
            this.color = color;
            return this;
        }

        @Override
        public StrikethroughPen build() {
            return new StrikethroughPen(color);
        }
    }

    public static final class UnderlinePenBuilder extends Builder{
        UnderlinePenBuilder() {}

        int color;

        public UnderlinePenBuilder setColor(int color) {
            this.color = color;
            return this;
        }

        @Override
        public UnderlinePen build() {
            return new UnderlinePen(color);
        }
    }
}
